<template>
  <div class="page-information">
    <white-header :isShowTopNav="true" :isShowBack="true" :isShowRight="true" title="新闻中心"></white-header>
    <div class="no-data" v-if="news_list.length == 0">
      <van-empty description="暂时没有新闻资讯哦~" />
    </div>
    <div class="information-center" v-if="news_list.length > 0">
      <news-card
        v-for="(item,index) in news_list"
        :key="index"
        :detail="item"
        :isShowBottomLine="(index+1) < news_list.length"
      ></news-card>
    </div>
    <div class="footer" v-if="news_list.length != 0 && news_list.length === total">
      <span class="tips">我是有底线的</span>
    </div>
  </div>
</template>
<script>
import { mapState, mapActions, mapMutations } from "vuex";
import WhiteHeader from "~/components/mobile/WhiteHeader.vue";
import { getNewsList } from "~/api/news";
import NewsCard from "~/components/mobile/NewsCard.vue";
export default {
  data() {
    return {
      news_list: [], //新闻资讯
      pageSize: 10,
      page: 1,
      total: 0,
    };
  },
  components: {
    NewsCard,
    WhiteHeader,
  },
  props: ["setDisabled", "refreshFun", "setLoading"],
  created() {
    this.setDisabled(false);
    this.getNews();
    this.refreshFun(() => {
      this.page = 1;
      this.getNews();
      this.setLoading(false);
    });
  },
  mounted() {},
  activated() {
    this.getNews();
    if (process.client) {
      window.addEventListener("scroll", this.eventListener);
    }
    this.setDisabled(false);
    this.refreshFun(() => {
      this.page = 1;
      this.getNews();
      this.setLoading(false);
    });
  },
  deactivated() {
    if (process.client) {
      window.removeEventListener("scroll", this.eventListener);
    }
    this.setDisabled(true);
  },
  destroyed() {
    if (process.client) {
      window.removeEventListener("scroll", this.eventListener);
    }
  },
  computed: {
    ...mapState({
      user_info(state) {
        return state.user_info;
      },
      is_loading(state) {
        return state.is_loading;
      },
    }),
  },
  methods: {
    ...mapMutations(["SET_IS_LOADING"]),
    eventListener() {
      this.throttle(this.pullUp, 300);
    },
    throttle(fn, delay) {
      let lastTime = 0;
      return !(function () {
        let nowTime = Date.now();
        if (nowTime - lastTime > delay) {
          fn.call(this);
          lastTime = nowTime;
        }
      })();
    },
    pullUp() {
      // 滚动条离顶部的距离
      let scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      // 窗口可视区域高度
      let clientHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      // 滚动区域高度
      let scrollHeight =
        document.documentElement.scrollHeight || document.body.scrollHeight;
      // 滚动条 + 可视区域高度 >= 滚动条高度 ==>  滚动条触底
      console.log(scrollTop + "+" + clientHeight + ">=" + scrollHeight);
      //适配处理:-120的是距离底部120px触发上拉加载，而不是触底加载
      if (scrollTop + clientHeight >= scrollHeight - 120) {
        // console.log("上拉加载1");
        // console.log(this.total);
        // console.log(this.news_list.length);
        if (this.total <= this.news_list.length) {
          return;
        }
        if (this.is_loading) {
          return;
        }
        this.page++;
        this.getNews("pullup");
        console.log("上拉加载2");
      }
    },
    formatDate(timestamp) {
      let date = new Date(timestamp);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      let hour = date.getHours();
      let minute = date.getMinutes();
      let second = date.getSeconds();
      let add0 = (m) => {
        return m < 10 ? "0" + m : m;
      };
      let showTime = null;
      if (new Date(timestamp).toDateString() === new Date().toDateString()) {
        if (hour < 10) {
          showTime = "上午";
        } else if (hour < 12) {
          showTime = "中午";
        } else if (hour < 18) {
          showTime = "下午";
        } else {
          showTime = "晚上";
        }
        // console.log("今天", showTime + " " + add0(hour) + ":" + add0(minute));
        return showTime + " " + add0(hour) + ":" + add0(minute);
      } else {
        // console.log("以前", add0(year) + "/" + add0(month) + "/" + add0(day));
        return add0(year) + "/" + add0(month) + "/" + add0(day);
      }
    },
    //获取新闻资讯
    async getNews(fetch_type) {
      this.SET_IS_LOADING(true);
      let params = {
        offset: this.pageSize * (this.page - 1),
        limit: this.pageSize,
        sort: "id",
        sortType: "desc",
      };
      let result = await getNewsList(params);
      if (result && result.data && result.data.code == 200) {
        if (fetch_type == "pullup") {
          this.news_list = [...this.news_list, ...result.data.data.rows];
        } else {
          this.news_list = result.data.data.rows;
        }
        this.total = result.data.data.total;
        console.log("news_list", result.data.data.rows);
      } else {
        
      }
      this.SET_IS_LOADING(false);
    },
  },
};
</script>
<style lang='scss' scoped>
.page-information {
  min-height: calc(100vh - 1.6rem);
  font-size: 0.3rem;
  //   padding: .3rem;
  .footer {
    padding: 0.3rem;
    font-size: 0.26rem;
    color: #babec2;
    text-align: center;
    .tips {
      position: relative;
    }
    .tips::before {
      content: "";
      display: block;
      position: absolute;
      left: -1.7rem;
      top: 50%;
      transform: translateY(-1px);
      width: 1.5rem;
      height: 1px;
      background: #babec2;
    }
    .tips::after {
      content: "";
      display: block;
      position: absolute;
      right: -1.7rem;
      top: 50%;
      width: 1.5rem;
      transform: translateY(-1px);
      height: 1px;
      background: #babec2;
    }
  }
}
</style>